package com.jic.market.impl;
import	java.util.Date;

import java.lang.Long;
import java.util.List;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.entity.MyBargain;
import com.jic.market.entity.ProductBargain;
import com.jic.market.mapper.PromotionMyBargainMapper;
import com.jic.market.mapper.PromotionProductBargainMapper;
import com.jic.market.request.ProductInfo;
import com.jic.market.request.rest.bargain.ProductBargainRestRequest;
import com.jic.market.response.rest.bargain.ProductBargainRestResponse;
import com.jic.market.service.ProductBargainService;
import com.jic.product.after.api.ProductListApi;
import com.jic.product.after.response.ProductAfterInfoByIdResponse;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 砍价活动商品关联表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-09-10 06:18:31
 */
@Service
public class ProductBargainServiceImpl implements ProductBargainService {

    @Autowired
    private PromotionProductBargainMapper productBargainMapper;

    @Autowired
    private ProductListApi productListApi;

    @Autowired
    private PromotionMyBargainMapper myBargainMapper;



    @Override
    public long insert(ProductBargain productBargain) {
        long flag =0;
        productBargain.setCreateTime(new Date());
        List<Long> productIds = productBargain.getProductIds();
        for(int i = 0; i < productIds.size(); i++){
             productBargain.setProductId(productIds.get(i));
             flag = productBargainMapper.insert(productBargain);
        }
        return flag;
    }

    @Override
    public long delete(Long id) {
        return productBargainMapper.delete(id);
    }

    @Override
    public long update(ProductBargain productBargain) {
        return productBargainMapper.update(productBargain);
    }

    @Override
    public ProductBargain queryByPrimaryKey(Long id) {
        return productBargainMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<ProductBargain> query(ProductBargain productBargain) {
        return productBargainMapper.query(productBargain);
    }

    @Override
    public PageResult<ProductBargain> queryPage(ProductBargain productBargain, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<ProductBargain> list = productBargainMapper.queryPage(startIndex, pageQuery.getLimit(), productBargain);
        long count = productBargainMapper.queryPageCount(productBargain);
        list.forEach(itmes->{
            RestResult<ProductAfterInfoByIdResponse> productAfterInfoByIdResponseRestResult = productListApi.queryProductInfoById(itmes.getProductId());
            ProductAfterInfoByIdResponse data = productAfterInfoByIdResponseRestResult.getData();
            itmes.setProductName(data.getChineseName());
            itmes.setCategory(data.getCategoryName());
            itmes.setProductCode(data.getCode());
//            itmes.setStock(data.getAvailableStock().toString());
            itmes.setPrice(data.getSalePriceWithTax().toString());
        });
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public RestResult insertPromotion(ProductBargain productBargain) {
        RestResult result = new RestResult();
        if (null != productBargain.getId()){
            productBargainMapper.update(productBargain);
        }else {
            productBargainMapper.insert(productBargain);
        }
        result.setCode("0000");
        return result;
    }

    @Override
    public PageResult<ProductBargainRestResponse> listRestPage(ProductBargainRestRequest productBargainRestRequest, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<ProductBargainRestResponse> list = productBargainMapper.listRestPage(startIndex, pageQuery.getLimit(), productBargainRestRequest);
        long count = productBargainMapper.listRestPageCount(productBargainRestRequest);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(item -> {
                //商品信息
                RestResult<ProductAfterInfoByIdResponse> responseRestResult =
                        productListApi.queryProductInfoById(item.getProductId());
                if (responseRestResult != null && responseRestResult.getData() != null) {
                    item.setPrice(responseRestResult.getData().getSalePriceWithTax());
                    item.setChineseName(responseRestResult.getData().getChineseName());
                    item.setUrl(responseRestResult.getData().getPictureUrl());
                }
                //多少人正在砍
                MyBargain myBargain=new MyBargain();
                myBargain.setDeleteFlag(0);
                myBargain.setBargainId(item.getBargainId());
                myBargain.setProductId(item.getProductId());
                item.setBargainNum(myBargainMapper.queryPageCount(myBargain));
            });
        }
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public RestResult updateProductBargain(ProductBargain productBargain) {
        List<ProductInfo> productInfo = productBargain.getProductInfo();
        for(int i = 0; i < productInfo.size(); i++){
            ProductBargain update =new ProductBargain();
            update.setId(productInfo.get(i).getId());
            update.setFloorPrice(productInfo.get(i).getFloorPrice());
            productBargainMapper.update(update);
        }
      return RestResult.success(1);
    }

    @Override
    public RestResult deleteProductBargain(ProductBargain productBargain) {
        for (int i = 0; i <productBargain.getProductIds().size(); i++ ){
            productBargainMapper.delete(productBargain.getProductIds().get(i));
        }
        return RestResult.success(1);
    }
}